﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Siparisler
{
    public partial class BedenDagilim : XForm
    {
        int CustomerId;
        public BedenDagilim(int CustomerId)
        {
            InitializeComponent();
            this.CustomerId = CustomerId;

            string customer = AppSession.Lookups.V_Customer.FindByCustomerId(CustomerId).Name;
            this.Text = customer + " Siparişleri";
            txtInfo.Caption = customer;    // "Sipariş Takip";            
        }

        private void BedenDagilim_Load(object sender, EventArgs e)
        {
            

            gridView1.Init("GetBedenler", CustomerId);

            gridView1.FormatAsNumber(0, "ToplamAdet");
            gridView1.AddSummary("ToplamAdet");

            //gridView1.SetFilterTypeToCheckedList("DosyaAdi", "Line", "DugmeKodu", "DugmeRengi", "YedekDugmeZarfi", "KolBoyu");

            for (int i = 1; i < 23; i++)
                gridView1.FormatAsNumber(1, String.Format("B{0}", i.ToString().PadLeft(2, '0')));

            for (int i = 1; i < 23; i++)
                gridView1.AddSummary(String.Format("B{0}", i.ToString().PadLeft(2, '0')));

            //RepositoryItemGridLookUpEdit gleNumune = GetCustomRI("Numune Üretim", false, "NUMUNE", "ÜRETİM", "KOLEKSİYON");
            //gridView1.Columns["NumuneUretim"].ColumnEdit = gleNumune;     


            gridView1.MakeReadOnly();
            gridView1.FocusedRowChanged+=gridView1_FocusedRowChanged;
            //gridView1.DoubleClick += gridView1_DoubleClick;
            gridView1.RowCellStyle += gridView1_RowCellStyle;
            gridView1.MakeRowSelectable();
            gridView1.BestFitColumns();
        }


        void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DagilimChanged();
        }



        int DagilimId = 0;
        private void DagilimChanged()
        {
            try
            {
                gridView1.CloseEditor();
                gridView1.BeginUpdate();

                if (DagilimId != gridView1.GetFocusedRowCellValue("DagilimId").ToInt(-1))
                {
                    DagilimId = gridView1.GetFocusedRowCellValue("DagilimId").ToInt(-1);

                    MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                    if (r != null)
                        for (int i = 1; i < 23; i++)
                        {
                            string fieldName = String.Format("B{0:00}", i);
                            string cap = r[String.Format("A{0:00}", i)].ToString();

                            //string Message = gridView1.Columns["Article"].ToString();
                            //gridView1.Columns[fieldName].Caption = "";
                            gridView1.Columns[fieldName].Caption = cap != string.Empty ? cap : " ";
                            //gridView1.Columns[fieldName].Caption = cap;
                        }
                }

            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gridView1.EndUpdate();
            }
        }

        //void gridView1_DoubleClick(object sender, EventArgs e)
        //{
        //    string model = gridView1.GetFocusedDataRow()["BareModel"].ToString();
        //    string linea = gridView1.GetFocusedDataRow()["Linea"].ToString();
        //    using (BedenDagilimTrendiModel m = new BedenDagilimTrendiModel(model, linea))
        //        m.ShowDialog();
        //}

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName.EndsWith("Style"))
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);

            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName.Length == 3 &&
                    e.Column.FieldName.StartsWith("B"))
                {
                    DataRow dr = gridView1.GetDataRow(e.RowHandle);
                    int D = intParse(dr["DagilimId"]);

                    e.Appearance.ForeColor = (DagilimId != D ? Color.LightSlateGray : Color.Black);
                }

            }
        }
    }
}
